﻿Imports System
Imports System.Reflection

Namespace System.Workflow.Activities
    Friend NotInheritable Class CorrelationPropertyValue
        ' Methods
        Friend Sub New(ByVal name As String, ByVal locationPath As String, ByVal signaturePosition As Integer)
            Me.name = name
            Me.locationPath = locationPath
            Me.signaturePosition = signaturePosition
        End Sub

        Private Function GetMemberType(ByVal mInfo As MemberInfo) As Type
            Dim memberType As MemberTypes = mInfo.MemberType
            If (memberType <> MemberTypes.Field) Then
                If (memberType <> MemberTypes.Property) Then
                    Return Nothing
                End If
            Else
                Return DirectCast(mInfo, FieldInfo).FieldType
            End If
            Return DirectCast(mInfo, PropertyInfo).PropertyType
        End Function

        Friend Function GetValue(ByVal args As Object()) As Object
            If (args.Length <= Me.signaturePosition) Then
                Throw New ArgumentOutOfRangeException("args")
            End If
            Dim target As Object = args(Me.signaturePosition)
            If (target Is Nothing) Then
                Return target
            End If
            Dim memberType As Type = target.GetType
            Dim obj3 As Object = target
            If (Me.locationPath.Length <> 0) Then
                Dim strArray As String() = Me.locationPath.Split(New Char() { "."c })
                Dim i As Integer
                For i = 1 To strArray.Length - 1
                    Dim name As String = strArray(i)
                    If (target Is Nothing) Then
                        Return obj3
                    End If
                    obj3 = memberType.InvokeMember(name, (BindingFlags.GetProperty Or (BindingFlags.GetField Or (BindingFlags.Public Or BindingFlags.Instance))), Nothing, target, Nothing, Nothing)
                    Dim member As MemberInfo() = memberType.GetMember(name, (BindingFlags.GetProperty Or (BindingFlags.GetField Or (BindingFlags.Public Or BindingFlags.Instance))))
                    memberType = Me.GetMemberType(member(0))
                    target = obj3
                Next i
            End If
            Return obj3
        End Function


        ' Properties
        Friend ReadOnly Property Name As String
            Get
                Return Me.name
            End Get
        End Property


        ' Fields
        Private locationPath As String
        Private name As String
        Private signaturePosition As Integer
    End Class
End Namespace

